home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / QuickDraw™ GX / Programming Stuff / Sample Code / Graphics Samples / ShapePart Browser ƒ / ShapeSetup.c < prev    next >
Encoding:
Text File  |  1995-04-10  |  8.7 KB  |  344 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    ShapeSetup.c
  3.  *
  4.  *    Robert Dierkes,  April 26, 1993
  5.  */
  6.  
  7.  
  8. #undef    MAC_HEADERS
  9.  
  10.  
  11. /*------------------*/
  12. /*    Include Files    */
  13. /*------------------*/
  14. #ifndef    MAC_HEADERS
  15.     #include <Memory.h>
  16.     #include <Windows.h>
  17.     #include <Controls.h>
  18. #endif    MAC_HEADERS
  19.  
  20. #include "graphics routines.h"
  21. #include "math routines.h"
  22. #include "graphics libraries.h"
  23. #include "font library.h"
  24.  
  25. #include "ShapeSetup.h"
  26.  
  27.  
  28. /*----------------------*/
  29. /*    Global Declarations    */
  30. /*----------------------*/
  31.  
  32.  
  33. /*------------------------------*/
  34. /*    External Declarations     */
  35. /*------------------------------*/
  36.  
  37.  
  38. /*------------------------------*/
  39. /*       Local ProtoTypes       */
  40. /*------------------------------*/
  41.  
  42.  
  43.     boolean
  44. CreateShapesFrame (WindowPtr pWindow, register long numRows, register long numCols, gxShape *pBoxSet)
  45. {
  46.     #define        kSixteenthInch    (IntToFixed (4) + kFixOneHalf)
  47.  
  48.     gxRectangle        bounds;
  49.     register
  50.     long            r, c;
  51.     gxShape            box;
  52.     fixed            side,
  53.                     left,
  54.                     top;
  55.  
  56.     if (numRows == 0  ||  numCols == 0)
  57.     {
  58.         DebugStr ("\pCreateShapesFrame: Number of rows or columns is zero");
  59.         return (false);
  60.     }
  61.  
  62.     /* Find side of each enclosing box */
  63.     side = FixedDivide (IntToFixed (pWindow->portRect.bottom - pWindow->portRect.top),
  64.                     IntToFixed (numCols)) - kSixteenthInch;
  65.  
  66.     *pBoxSet = GXNewShape (gxEmptyType);
  67.     for (r = 0, top = kSixteenthInch + (kSixteenthInch>>1); r < numRows; r++, top += side)
  68.     {
  69.         for (c = 0, left = kSixteenthInch + (kSixteenthInch>>1); c < numCols; c++, left += side)
  70.         {
  71.             box = NewShape4 (gxRectangleType, left, top, left + side, top + side);
  72.             GXInsetShape (box, kSixteenthInch);
  73.             GXSetShapeFill (box, gxEvenOddFill);
  74.             AddToShape (*pBoxSet, box);
  75.             GXDisposeShape (box);
  76.         }
  77.     }
  78.     SetShapeCommonColor (*pBoxSet, grayish + silver);
  79.  
  80.     return (true);
  81. }
  82.  
  83.  
  84.     boolean
  85. CreateHitTestShapes (WindowPtr pWindow, gxShape **h1stHitShape, gxShape *pBoxSet, register long shapeCount)
  86. {
  87.     gxShape            *pShape;
  88.     register
  89.     long            shapeNum;
  90.     gxRectangle        bounds;
  91.  
  92.     if (shapeCount <= 0)
  93.     {
  94.         DebugStr ("\pCreateHitTestShapes: Number of shapes is <= zero");
  95.         return (false);
  96.     }
  97.  
  98.     if (h1stHitShape == nil)
  99.     {
  100.         DebugStr ("\pCreateHitTestShapes: Shape list handle is nil");
  101.         return (false);
  102.     }
  103.  
  104.     /* Allocate space for hit shapes */
  105.     *h1stHitShape = pShape = (gxShape *) NewPtr (shapeCount * sizeof (gxShape));
  106.     if (*h1stHitShape == nil)
  107.     {
  108.         DebugStr ("\pCreateHitTestShapes: NewPtr for shapes failed");
  109.         return (false);
  110.     }
  111.  
  112.     for (shapeNum = 0; shapeNum < shapeCount; shapeNum++, pShape++)
  113.     {
  114.         switch (shapeNum)
  115.         {
  116.         case kTrapizoidPolygon:
  117.             *pShape = NewShape4 (gxRectangleType, 0, 0, kHalfInch, kHalfInch);
  118.             SkewShapeAboutCenter (*pShape, -kFixOneHalf, 0);
  119.             SetShapeCommonColor (*pShape, light + blue);
  120.             break;
  121.  
  122.         case kOvalPath:
  123.             bounds.left        =
  124.             bounds.top        = 0;
  125.             bounds.right    =
  126.             bounds.bottom    = kHalfInch;
  127.             *pShape = NewOval (&bounds);
  128.  
  129.             GXSetShapeFill (*pShape, gxClosedFrameFill);
  130.             GXSetShapePen (*pShape, IntToFixed (15));
  131.             SetShapeCommonColor (*pShape, rose_madder);
  132.             break;
  133.  
  134.         case kArrowPolygon:
  135.             {
  136.                 fixed    arrow[] = {1L, 7L,    0,                IntToFixed (5),
  137.                                             0,              IntToFixed (15),
  138.                                               IntToFixed (12),        IntToFixed (14),
  139.                                                 IntToFixed (10),    IntToFixed (20),
  140.                                                   IntToFixed (27),    IntToFixed (10),
  141.                                                 IntToFixed (10),    0,
  142.                                               IntToFixed (12),        IntToFixed (6)};
  143.                 gxJoinRecord    joinRec;
  144.  
  145.                 *pShape    = GXNewPolygons ((gxPolygons *) arrow);
  146.  
  147.                 joinRec.attributes = gxCurveJoin;
  148.                 joinRec.join = nil;
  149.                 joinRec.miter = 0;
  150.                 GXSetShapeJoin (*pShape, &joinRec);
  151.                 GXSetShapeFill (*pShape, gxClosedFrameFill);
  152.                 GXSetShapePen (*pShape, IntToFixed (12));
  153.  
  154.                 RotateShapeAboutCenter (*pShape, IntToFixed (-45));
  155.                 SetShapeCommonColor (*pShape, turquoise);
  156.             }
  157.             break;
  158.  
  159.         case kPatternedPolygon:
  160.             {
  161.                 fixed    star[] = {1L, 5L,    IntToFixed (3),    IntToFixed (18),
  162.                                             IntToFixed (9),    0,
  163.                                             IntToFixed (15),IntToFixed (18),
  164.                                             0,                IntToFixed (6),
  165.                                             IntToFixed (18),IntToFixed (6)};
  166.                 gxPatternRecord    patternRec;
  167.  
  168.                 *pShape = NewShape4 (gxRectangleType, 0, 0, kHalfInch, kHalfInch);
  169.                 RotateShapeAboutCenter (*pShape, IntToFixed (-6));
  170.                 SetShapeCommonColor (*pShape, gxWhite);
  171.  
  172.                 patternRec.pattern = GXNewPolygons ((gxPolygons *) star);
  173.                 GXSetShapeFill (patternRec.pattern, gxWindingFill);
  174.                 patternRec.u.x =  kQuarterInch;
  175.                 patternRec.u.y = -kQuarterInch;
  176.                 patternRec.v.x =  kQuarterInch;
  177.                 patternRec.v.y =  kEigthInch;
  178.                 GXSetShapePattern (*pShape, &patternRec);
  179.                 GXDisposeShape (patternRec.pattern);
  180.             }
  181.             break;
  182.  
  183.         case kWordGlyphs:
  184.             {
  185.                 #define    kText    "GX"
  186.                 gxPoint    position = {0, 0};
  187.  
  188.                 *pShape = NewCString (kText, &position);
  189.                 GXSetShapeType (*pShape, gxGlyphType);
  190.                 GXSetShapeTextSize (*pShape, IntToFixed (66));
  191.                 SetStyleCNamedFont (GXGetShapeStyle (*pShape), "Hoefler Italic");
  192.                 SetShapeCommonColor (*pShape, indigo);
  193.             }
  194.             break;
  195.  
  196.         case kArchCurve:
  197.             {
  198.                 fixed    arch[] = {    0,                kHalfInch,
  199.                                     kEigthInch,        0,
  200.                                     kQuarterInch,    kHalfInch};
  201.  
  202.                 *pShape = GXNewCurve ((gxCurve *) arch);
  203.                 GXSetShapePen (*pShape, IntToFixed (20));
  204.                 SetShapeCommonColor (*pShape, light + maroon);    /* light + violet */
  205.                 ScaleShapeAboutCenter (*pShape, 5 * fixed1, 3 * fixed1);
  206.             }
  207.             break;
  208.  
  209.         case kCapsLine:
  210.             {
  211.                 #define    kFixOneQuarter    (fixed1 >> 2)
  212.                 #define    kFix3Quarters    ((3 * fixed1) >> 2)
  213.  
  214.                 fixed    star[] = {1L, 5L,    -fixed1-kFixOneQuarter,    -3*fixed1+kFixOneQuarter,
  215.                                             -fixed1-kFixOneQuarter,     2*fixed1+kFixOneHalf,
  216.                                              fixed1+kFix3Quarters,    -2*fixed1,
  217.                                             -3*fixed1,                 0,
  218.                                              fixed1+kFix3Quarters,     fixed1+kFix3Quarters};
  219.                 gxCapRecord    cap;
  220.                 gxRectangle    endRect;
  221.  
  222.                 *pShape = NewShape4 (gxLineType, fixed1, 0, 0, fixed1);
  223.                 GXSetShapePen (*pShape, IntToFixed (8));
  224.                 SetShapeCommonColor (*pShape, viridian_light);
  225.  
  226.                 endRect.left   = 0;
  227.                 endRect.top    = kFix3Quarters;
  228.                 endRect.right  = 2 * kFix3Quarters;
  229.                 endRect.bottom = -kFix3Quarters;
  230.                 cap.endCap   = GXNewRectangle (&endRect);
  231.                 cap.startCap = GXNewPolygons ((gxPolygons *) star);
  232.  
  233.                 cap.attributes = 0;
  234.                 GXSetShapeCap (*pShape, &cap);
  235.                 GXDisposeShape (cap.startCap);
  236.                 GXDisposeShape (cap.endCap);
  237.             }
  238.             break;
  239.  
  240.         case kDashedOvalPath:
  241.             {
  242.                 gxDashRecord    dashRec;
  243.  
  244.                 bounds.left        =
  245.                 bounds.top        = 0;
  246.                 bounds.right    =
  247.                 bounds.bottom    = kQuarterInch;
  248.  
  249.                 dashRec.attributes = gxBreakDash + gxAutoAdvanceDash;
  250.                 dashRec.dash    = NewOval (&bounds);
  251.                 dashRec.advance    = kHalfInch;
  252.                 dashRec.phase    = 0;
  253.                 dashRec.scale    = kQuarterInch;
  254.  
  255.                 bounds.left        =
  256.                 bounds.top        = 0;
  257.                 bounds.right    =
  258.                 bounds.bottom    = kHalfInch + kEigthInch;
  259.                 *pShape = NewOval (&bounds);
  260.  
  261.                 GXSetShapeDash (*pShape, &dashRec);
  262.                 GXSetShapeFill (*pShape, gxClosedFrameFill);
  263.                 GXSetShapeStyleAttributes (*pShape, gxOutsideFrameStyle);
  264.                 GXSetShapePen (*pShape, kQuarterInch);
  265.                 SetShapeCommonColor (*pShape, light + apple_yellow);
  266.                 GXDisposeShape (dashRec.dash);
  267.             }
  268.             break;
  269.  
  270.         case kJoinPath:
  271.             {
  272.                 fixed    spokes[] = {1L, 8L, 0x55000000,                0,   IntToFixed (4),
  273.                                                         IntToFixed (4),  IntToFixed (4),
  274.                                                         IntToFixed (4),                0,
  275.                                                         IntToFixed (4),  IntToFixed (4),
  276.                                                         IntToFixed (8),  IntToFixed (4),
  277.                                                         IntToFixed (4),  IntToFixed (4),
  278.                                                         IntToFixed (4),  IntToFixed (8),
  279.                                                         IntToFixed (4),  IntToFixed (4)};
  280.                 *pShape    = GXNewPaths ((gxPaths *) spokes);
  281.                 SetShapeCommonColor (*pShape, light + orange);
  282.             }
  283.             break;
  284.         }
  285.  
  286.         if (*pShape)
  287.         {
  288.             GXGetShapeBounds (*pBoxSet, shapeNum + 1, &bounds);
  289.  
  290.             if (shapeNum == kWordGlyphs)
  291.                 GXMoveShapeTo (*pShape, bounds.left + kEigthInch, bounds.bottom - kHalfInch - kEigthInch);
  292.             else if (shapeNum == kArchCurve)
  293.                 CenterShape (*pShape, &bounds);
  294.             else
  295.                 GXSetShapeBounds (*pShape, &bounds);
  296.  
  297.             if (shapeNum == kTrapizoidPolygon  ||
  298.                 shapeNum == kOvalPath  ||
  299.                 shapeNum == kPatternedPolygon  ||
  300.                 shapeNum == kDashedOvalPath)
  301.                 GXInsetShape (*pShape, kQuarterInch);
  302.  
  303.             if (shapeNum == kArrowPolygon)
  304.                 ScaleShapeAboutCenter (*pShape, kFixOneHalf + (kFixOneHalf >> 1), kFixOneHalf + (kFixOneHalf >> 1));
  305.  
  306.             if (shapeNum == kCapsLine)
  307.                 ScaleShapeAboutCenter (*pShape, kFixOneHalf, kFixOneHalf);
  308.  
  309.             if (shapeNum == kJoinPath)
  310.                 ScaleShapeAboutCenter (*pShape, 7 * (kFixOneHalf >> 2), 7 * (kFixOneHalf >> 2));
  311.  
  312.             GXCacheShape (*pShape);
  313.         }
  314.     }
  315.  
  316.     return (true);
  317. }
  318.  
  319.  
  320.     void
  321. DisposeHitTestShapes (gxShape **h1stHitShape, gxShape *pBoxes)
  322. {
  323.     register
  324.     gxShape    *pShape;
  325.     long    shapeCount;
  326.  
  327.     if (pBoxes  &&  *pBoxes)
  328.     {
  329.         if (h1stHitShape  &&  *h1stHitShape)
  330.         {
  331.             shapeCount = GXCountShapeContours (*pBoxes);
  332.             pShape = *h1stHitShape + shapeCount - 1;
  333.  
  334.             while (shapeCount--)
  335.                 DisposeShapeAt (pShape--);
  336.  
  337.             DisposePtr ((Ptr) *h1stHitShape);
  338.             *h1stHitShape = nil;
  339.         }
  340.  
  341.         DisposeShapeAt (pBoxes);
  342.     }
  343. }
  344.